/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.swta.freezay.database.controller;

import de.swta.freezay.database.controller.exceptions.NonexistentEntityException;
import de.swta.freezay.database.controller.exceptions.PreexistingEntityException;
import de.swta.freezay.database.dbEntities.Reservation;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import de.swta.freezay.database.dbEntities.Package;

/**
 *
 * @author Christian
 */
public class ReservationJpaController {

    public ReservationJpaController() {
        emf = Persistence.createEntityManagerFactory("FreezayPU");
    }
    private EntityManagerFactory emf = null;

    public List<Reservation> findReservationEntities(int maxResults, int firstResult,
            String sortBy, boolean ascending) {

        EntityManager em = getEntityManager();
        try {

            Query q = em.createQuery("select object(o) from Reservation as o order "
                    + "by o." + sortBy + " " + (ascending ? "ASC" : "DESC"));
            
            q.setMaxResults(maxResults);
            q.setFirstResult(firstResult);

            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Reservation reservation) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Package package1 = reservation.getPackage1();
            if (package1 != null) {
                package1 = em.getReference(package1.getClass(), package1.getId());
                reservation.setPackage1(package1);
            }
            em.persist(reservation);
            if (package1 != null) {
                package1.getReservationCollection().add(reservation);
                package1 = em.merge(package1);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findReservation(reservation.getReservationNr()) != null) {
                throw new PreexistingEntityException("Reservation " + reservation + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Reservation reservation) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reservation persistentReservation = em.find(Reservation.class, reservation.getReservationNr());
            Package package1Old = persistentReservation.getPackage1();
            Package package1New = reservation.getPackage1();
            if (package1New != null) {
                package1New = em.getReference(package1New.getClass(), package1New.getId());
                reservation.setPackage1(package1New);
            }
            reservation = em.merge(reservation);
            if (package1Old != null && !package1Old.equals(package1New)) {
                package1Old.getReservationCollection().remove(reservation);
                package1Old = em.merge(package1Old);
            }
            if (package1New != null && !package1New.equals(package1Old)) {
                package1New.getReservationCollection().add(reservation);
                package1New = em.merge(package1New);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = reservation.getReservationNr();
                if (findReservation(id) == null) {
                    throw new NonexistentEntityException("The reservation with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reservation reservation;
            try {
                reservation = em.getReference(Reservation.class, id);
                reservation.getReservationNr();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The reservation with id " + id + " no longer exists.", enfe);
            }
            Package package1 = reservation.getPackage1();
            if (package1 != null) {
                package1.getReservationCollection().remove(reservation);
                package1 = em.merge(package1);
            }
            em.remove(reservation);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Reservation> findReservationEntities() {
        return findReservationEntities(true, -1, -1);
    }

    public List<Reservation> findReservationEntities(int maxResults, int firstResult) {
        return findReservationEntities(false, maxResults, firstResult);
    }

    private List<Reservation> findReservationEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Reservation as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Reservation findReservation(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Reservation.class, id);
        } finally {
            em.close();
        }
    }

    public int getReservationCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from Reservation as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
